/*
 *  AParamItem.cpp
 *  libavid
 *
 *  Created by Peter on 25.02.12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "AParamItem.h"
#include "AStream.h"

A_IMPLEMENT_CLASS( 'PRIT', AParamItem, AObject );

void AParamItem::readFromStream( AStream& str, const AObjDoc& objd )
{
	UInt8 tagMark, version;
	str.readPod( tagMark );
	str.readPod( version );
	TR_ASSERT( tagMark == 0x02 );
	TR_ASSERT( version == 0x02 );
	
	// Read the guid (stored simply as binary data without any TagMarks)
	_guid.readFromStream( str );
	
	// Enum member, but stored as Int16
	Int16 valueType;
	str.readPod( valueType );
	_valueType = static_cast< ValueType >( valueType );
	
	switch( _valueType )
	{
		case VALUE_TYPE_LONG:
			str.readPod( _value._longValue );
			break;
		case VALUE_TYPE_DOUBLE:
			str.readPod( _value._doubleValue );
			break;
		default:
			TR_TRACE( "AParamItem: Unknown value type " << _valueType );
			throw "Unknown value type";
	}
	
	_name = str.readString();
	_enabled = str.readBool();
	
	str.readPod( _controlTrack );
	
	// Read PClass Ext ContributesToSignature
	str.readPod( tagMark );
	TR_ASSERT( tagMark == 0x01 );
	str.readPod( version );
	TR_ASSERT( version == 0x01 );
	str.readPod( tagMark );
	TR_ASSERT( tagMark == 66 );
	
	_contributesToSignature = str.readBool();
	
	str.readPod( tagMark );
	TR_ASSERT( tagMark == 0x03 );
}

#define TRACE_FIELD( x ) "\t" #x ": " << x << std::endl

void AParamItem::dump( std::ostream& str ) const
{
	str << "AParamItem {" << std::endl;
	str << TRACE_FIELD( _guid );
	
	switch( _valueType )
	{
		case VALUE_TYPE_LONG:
			str << "\t_valueType: VALUE_TYPE_LONG" << std::endl;
			str << "\t_value: " << _value._longValue << std::endl;
			break;
		case VALUE_TYPE_DOUBLE:
			str << "\t_valueType: VALUE_TYPE_DOUBLE" << std::endl;
			str << "\t_value: " << _value._doubleValue << std::endl;
			break;
		default:
			str << "\t_valueType: <unknown:" << _valueType << ">" << std::endl;
			str << "\t_value: " << "<n/a>" << std::endl;
			break;
	}

	str << TRACE_FIELD( _name );
	str << TRACE_FIELD( _enabled );
	str << TRACE_FIELD( _controlTrack );
	str << TRACE_FIELD( _contributesToSignature );
	str << "}";
}